/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gr.anagnosg.general.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 *
 * @author anagnosg
 */
public class HashMapOrder {
 LinkedHashMap hm ;

    public LinkedHashMap getHm() {
        return hm;
    }

    public void setHm(LinkedHashMap hm) {
        this.hm = hm;
    }
 
 public HashMapOrder(LinkedHashMap hmp ){
     this.hm = hmp;
 }
 
 public int getIndex(Object keyP){
    List keyList = new ArrayList(hm.keySet());
    int index = keyList.indexOf(keyP);
    return index;
 }
 public boolean canGoUp(Object keyP){
     List keyList = new ArrayList(hm.keySet());
     int index = keyList.indexOf(keyP);
     if(index!=0) return true;
     else return false;
 }
 
  public boolean canGoDown(Object keyP){
     List keyList = new ArrayList(hm.keySet());
     int index = keyList.indexOf(keyP);
     if(index!=keyList.size()-1) return true;
     else return false;
 }
 
 public void elementUp(Object keyP){//decrease index.
     LinkedHashMap newHm = new LinkedHashMap();
     List keyList = new ArrayList(hm.keySet());
     int index = keyList.indexOf(keyP);
     if(index!=0){
        Object value = hm.get(keyP);
        Object previousKey = keyList.get(index-1);
        Object valuePrevious = hm.get(previousKey);
     
        int j = 0 ; 
        for(Object key:hm.keySet()){
            if(j==index-1){
                newHm.put(keyP, value);
            }
            else if(j==index){
                newHm.put(previousKey, valuePrevious);
            }
            else {
                newHm.put(key, hm.get(key));
            }
            j++;
        }
        this.hm=newHm;
     }
 }
 
 public void elementDown(Object keyP){//decrease index.
     LinkedHashMap newHm = new LinkedHashMap();
     List keyList = new ArrayList(hm.keySet());
     int index = keyList.indexOf(keyP);
     if(index!=keyList.size()){
        Object value = hm.get(keyP);
        Object nextKey = keyList.get(index+1);
        Object valueNext = hm.get(nextKey);
     
        int j = 0 ; 
        for(Object key:hm.keySet()){
            if(j==index){
                newHm.put(nextKey, valueNext);
            }
            else if(j==index+1){
                newHm.put(keyP, value);
            }
            else {
                newHm.put(key, hm.get(key));
            }
            j++;
        }
        this.hm=newHm;
     }
     
 }
 
}
